package com.brightyoyo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * @author bri
 * @since 15-8-18.
 */
public class MatchingRateView extends View implements MatchingRateDrawer {

    private static final String MATCHING_RATE = "匹配度";
    // "匹配度"与圆心的偏移量
    private static final float RATE_OFFSET = 14;
    private static final float RATE_TEXT_SIZE = 14;
    private static final float PERCENT_TEXT_SIZE = 18;
    // 匹配度和百分比之间的间距
    private static final float MARGIN = 2;

    // 匹配度画笔
    private Paint mRatePaint;
    // 匹配度百分比
    private CharSequence mPercentText = "0";

    // 百分比画笔
    private Paint mPercentPaint;

    // 控件背景颜色
    private int mBgColor = Color.RED;

    public MatchingRateView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public MatchingRateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public MatchingRateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        initPaint();

        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MatchingRateView);
        mPercentText = ta.getString(R.styleable.MatchingRateView_android_text);
        mPercentText = TextUtils.isEmpty(mPercentText) ? "0" : mPercentText;
        mBgColor = ta.getColor(R.styleable.MatchingRateView_android_background, mBgColor);
        ta.recycle();
    }

    private void initPaint() {

    }

    @Override public void setMatchingRate(int rate, int bg) {

    }

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 72, getResources().getDisplayMetrics());
        int measureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
        setMeasuredDimension(measureSpec, measureSpec);
    }

    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        final float width = getMeasuredWidth();
        final float height = getMeasuredHeight();

        final float cx = width / 2;
        final float cy = height / 2;
        final float radius = width / 2;

        CircleDrawable circleDrawable = new CircleDrawable(
                mBgColor,
                width / 2,
                height / 2,
                width / 2);
        circleDrawable.draw(canvas);

        mRatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRatePaint.setColor(Color.WHITE);
        mRatePaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, RATE_TEXT_SIZE, getResources().getDisplayMetrics()));
        mRatePaint.setTextAlign(Paint.Align.CENTER);

        final float baseRateX = radius;
        final float baseRateY = radius - RATE_OFFSET;

        canvas.drawText(
                MATCHING_RATE,
                baseRateX,
                baseRateY,
                mRatePaint);


        mPercentPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPercentPaint.setColor(Color.WHITE);
        final float percentTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, PERCENT_TEXT_SIZE, getResources().getDisplayMetrics());
        mPercentPaint.setTextSize(percentTextSize);
        mPercentPaint.setTextAlign(Paint.Align.CENTER);

        final float margin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MARGIN, getResources().getDisplayMetrics());
        final float basePerX = baseRateX;
        final float basePerY = baseRateY + percentTextSize + margin;
        canvas.drawText(
                mPercentText + "%",
                basePerX,
                basePerY,
                mPercentPaint
        );
    }

    @Override public void setBackgroundDrawable(Drawable background) {

    }

    private class CircleDrawable extends Drawable {

        private final float mCircleRadius;
        private Paint mPaint;
        private int mOvalColor;
        private float mCx, mCy;

        public CircleDrawable(int color, float cx, float cy, float radius) {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
            mPaint.setStyle(Paint.Style.FILL);

            mCx = cx;
            mCy = cy;

            mOvalColor = color;
            mCircleRadius = radius;
        }

        @Override public void draw(Canvas canvas) {
            mPaint.setColor(mOvalColor);
            canvas.drawCircle(mCx, mCy, mCircleRadius, mPaint);
        }

        @Override
        public void setAlpha(int alpha) {
            mPaint.setAlpha(alpha);
        }

        @Override
        public void setColorFilter(ColorFilter cf) {
            mPaint.setColorFilter(cf);
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }
    }

}
